/* 字面量类型 */
let changingString = "Hello World";
changingString = "Olá Mundo";
// Because `changingString` can represent any possible string, that
// is how TypeScript describes it in the type system
changingString;
// let changingString: string


const constantString = "Hello World";
// Because `constantString` can only represent 1 possible string, it
// has a literal type representation
constantString;
// const constantString: "Hello World"


let x: "hello" = "hello";
// OK
x = "hello";
// ...
x = "howdy";
// Error：Cannot assign to 'x' because it is a constant.ts(2588)


function printText(s: string, alignment: "left" | "right" | "center") {
  // ...
}
printText("Hello, world", "left");
printText("G'day, mate", "centre");
// Argument of type '"centre"' is not assignable to parameter of type '"left" | "right" | "center"'.


function compare(a: string, b: string): -1 | 0 | 1 {
  return a === b ? 0 : a > b ? 1 : -1;
}
interface Options {
  width: number;
}
function configure(x: Options | "auto") {
  // ...
}
configure({ width: 100 });
configure("auto");
configure("automatic");
// Error：Argument of type '"automatic"' is not assignable to parameter of type 'Options | "auto"'.

// 字面量推断===============================================================================================================
const obj11 = { counter: 0 };
if (1 == 1) {
  obj11.counter = 1;
}


declare function handleRequest(url: string, method: "GET" | "POST"): void;
const req = { url: "https://example.com", method: "GET" };
// Argument of type 'string' is not assignable to parameter of type '"GET" | "POST"'.
handleRequest(req.url, req.method);
// 使用类型断言改变推断结果
const req11 = { url: "https://example.com", method: "GET" as "GET" };
handleRequest(req11.url, req11.method);
// 使用 as const 把整个对象转为一个类型字面量
const req112 = { url: "https://example.com", method: "GET" } as const;
handleRequest(req112.url, req112.method);